package com.yc.mvc.web;

import com.yc.mvc.Person;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/8/9
 * Email haijunzhou@hnit.edu.cn
 */
//@Controller     // 默认每个方法返回结果跳转页面 ( jsp, html)
@RestController   // 默认每个方法返回结果自动转 json
public class IndexController {

    @RequestMapping("hello")
    String hello(String name){
        return "hello " + name;
    }

    @RequestMapping("a/goIdx")
    ModelAndView toIndex( ModelAndView mav){
        mav.setViewName("redirect:../index.html");
        return mav;
    }

    @RequestMapping(path = "doget", method = RequestMethod.GET)
    // @GetMapping => @RequestMapping(method = RequestMethod.GET)
    // @PostMapping
    String doGet(){
        return "doGet";
    }

    @RequestMapping(path = "dogetOrPost",
            method = {RequestMethod.GET,RequestMethod.POST})
    String doGetOrPost(){
        return "doGet + doPost";
    }

    @RequestMapping(path="add",params = { "a!=100", "b=200" })
    int add(int a, int b){
        return a + b;
    }

    @RequestMapping(path = "host", headers = "Host=localhost:8080")
    String host(){
        return "localhost:8080";
    }

    @RequestMapping(path="consume",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    String consume(){
        return "consume";
    }

    @RequestMapping(value = "produce1", produces = "application/json;charset=utf-8")
    String produce1(){
        // resp.setContentType("");
        return "<h1>你好中国!</h1>";
    }

    @RequestMapping(value = "produce2", produces = "text/html;charset=utf-8")
    String produce2(){
        // resp.setContentType("");
        return "<h1>你好中国!</h1>";
    }

    ///////////////////////////////////
    // sub?x=5&y=3
    @GetMapping("sub")
    public int sub(
            @RequestParam(name="x", defaultValue = "5") int a,
            @RequestParam(value="y", required = true) int b){
        return a - b;
    }

    ////////////////////////////

    @GetMapping("path/{x}/{y}")
    public int path(
            @PathVariable("y") int a ,
            @PathVariable(name="x", required = true) int b){
        return a * b;
    }

    @GetMapping("headers")
    public Map<String,Object> headers(
            @RequestHeader String Host,
            @RequestHeader("Accept")String accept,
            @RequestHeader(name="Connection", required = false) String conn,
            @RequestHeader(name="abc", defaultValue = "100") String abc,
            @CookieValue String name,
            @CookieValue(name="myage",defaultValue = "18") int age){
        Map<String,Object>ret = new HashMap<>();
        ret.put("Host",Host);
        ret.put("accept",accept);
        ret.put("conn",conn);
        ret.put("abc",abc);
        ret.put("name",name);
        ret.put("age",age);
        return ret;
    }

    @RequestMapping("body")
    String body(@RequestBody String body){
        System.out.println("body = " + body);  // JSON
        return body;
    }

    @RequestMapping("body1")
    Person body(@RequestBody Person person){
        System.out.println("person = " + person);  // JSON
        return person;
    }

    @RequestMapping("body2")
    Person body2(Person person){
        System.out.println("person = " + person);  // JSON
        return person;
    }

}
